﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MigrationTool
{
    public partial class ObjTableForm : Form
    {
        public ObjTableForm()
        {
            InitializeComponent();
        }


        private const string sql0 = @"Select so.id, so.name tablename
  from sysobjects so 
 WHERE
 so.type='U' 
  ORDER BY  tablename";

        private SqlConnection getConnection()
        {
            SqlConnection connection;
            dynamic o = this.Tag;
            if (o.AuthType == 0)
            {
                connection = new SqlConnection(string.Format("Server={0};Integrated Security=SSPI;Database={1}", o.ServerName, o.Db));
            }
            else
            {
                connection = new SqlConnection(string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=False;User ID={2};Password={3}"
                    , o.ServerName, o.Db, o.User, o.Pass));
            }
            return connection;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            try
            {
                using (SqlConnection conn = getConnection())
                {
                    //String sqlQuery = sql0;
                    // open the connection
                    conn.Open();

                    using (SqlCommand cmd = new SqlCommand(sql0, conn))
                    {
                        // execute query and read data in SqlDataReader
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            DataTable dt = new DataTable();
                            // call load method of datatable to copy content of reader 
                            dt.Load(reader); // Load method does not close the connnection. In this case, the using statement does.
                            lstTables.DisplayMember = "tablename";
                            lstTables.ValueMember = "id";
                            lstTables.DataSource = dt;
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                (this.Owner as MainForm).Appendlog(string.Format("不能导出样本:{0}", ex.Message),Color.DarkRed);
                this.Close();
            }


        }

        private void ObjTableForm_Load(object sender, EventArgs e)
        {


        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void lstTables_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A && e.Control)
            {
                //for (int i = 0; i < lstTables.Items.Count; i++)
                //{

                //    lstTables.SelectedIndex = i;
                //}   
                for (int i = 0; i < lstTables.Items.Count; i++)
                {

                    lstTables.SetSelected(i, true);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
